package xyz.yeidi.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import xyz.yeidi.pojo.Orders;
import xyz.yeidi.service.api.OrdersService;
import xyz.yeidi.utils.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author JIP
 * @Date 2021-08-07 22:09
 * @Version 1.0
 * @Describe :
 */
@WebServlet("/getAllOrdersByUserId")
public class GetAllOrdersByUserIdServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrdersService ordersService = ServiceFactory.getOrdersService();
        String userId = request.getParameter("userId");
        PrintWriter out = response.getWriter();
        ObjectMapper om = new ObjectMapper();
        List<Orders> list = ordersService.getOrdersByUserId(Integer.valueOf(userId));
        if(list==null || list.isEmpty()){
            out.print("no_order");
        }else{
            out.print(om.writeValueAsString(list));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
